﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Thu Vien Them Vao
using CrystalDecisions.Shared;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;
using System.Globalization;
//using CrystalDecisions.CrystalReports.Engine;
//using CrystalDecisions.Shared;

namespace QuanLyQuanCafe
{
    public partial class FrmMain : Form
    {
        KhuVucBL khuvucBL = new KhuVucBL();
        BanBL banBL = new BanBL();
        private NhomMonBL nhommonBL = new NhomMonBL();
        private MonAnBL monanBL = new MonAnBL();
        private KhuyenMaiBL khuyenmaiBL = new KhuyenMaiBL();
        private CT_KhuyenMaiBL ctkhuyenmaiBL = new CT_KhuyenMaiBL();
        private HoaDonBL hoadonbl = new HoaDonBL();
        private CT_HoaDonBL cthoadonbl = new CT_HoaDonBL();
        private CT_DenBuBL ctdenbubl = new CT_DenBuBL();
        Button newbutton;

        public FrmDangNhap dangnhap;
        public FrmMain( FrmDangNhap _dangnhap)
        {
            InitializeComponent();

            dangnhap = _dangnhap;
        }
        
        private void menuitemNhanVien_Click(object sender, EventArgs e)
        {
            FrmNhanVien frmNhanVien = new FrmNhanVien();
            frmNhanVien.ShowDialog();
        }

        private void menuitemKhachHang_Click(object sender, EventArgs e)
        {
            FrmKhachHang frmkhachhang = new FrmKhachHang();
            frmkhachhang.ShowDialog();
        }

        private void menuitemKhuVuc_Click(object sender, EventArgs e)
        {
            FrmKhuVuc frmkhuvuc = new FrmKhuVuc();
            frmkhuvuc.ShowDialog();
            LoadBanKhuVuc();
        }

        private void menuitemBan_Click(object sender, EventArgs e)
        {
            FrmBan frmban = new FrmBan();
            frmban.ShowDialog();
            LoadBanKhuVuc();
        }

        private void menuitemNhomMon_Click(object sender, EventArgs e)
        {
            FrmNhomMon frmnhom = new FrmNhomMon();
            frmnhom.ShowDialog();
            LoadThucAn();
        }

        private void menuitemNhaCC_Click(object sender, EventArgs e)
        {
            FrmNhaCungCap frmnhacungcap = new FrmNhaCungCap();
            frmnhacungcap.ShowDialog();
        }

        private void menuitemDVT_Click(object sender, EventArgs e)
        {
            FrmDonViTinh frmdonvitinh = new FrmDonViTinh();
            frmdonvitinh.ShowDialog();
        }

        private void menuitemNL_Click(object sender, EventArgs e)
        {
            FrmNguyenLieu frmnguyenlieu = new FrmNguyenLieu();
            frmnguyenlieu.ShowDialog();
        }

        private void menuitemMonAn_Click(object sender, EventArgs e)
        {
            FrmMonAn frmmonan = new FrmMonAn();
            frmmonan.ShowDialog();
            LoadThucAn();
        }

        private void menuitemTraCuuNhanVien_Click(object sender, EventArgs e)
        {
            FrmTraCuuNhanVien frmtracuunhanvien = new FrmTraCuuNhanVien();
            frmtracuunhanvien.ShowDialog();
        }

        private void menuitemTraCuuKhachHang_Click(object sender, EventArgs e)
        {
            FrmTraCuuKhachHang frmtracuukhachhang = new FrmTraCuuKhachHang();
            frmtracuukhachhang.ShowDialog();
        }

        private void menuitemNhapKho_Click(object sender, EventArgs e)
        {
            FrmNhapKho frmnhapkho = new FrmNhapKho();
            frmnhapkho.ShowDialog();
        }

        private void menuitemTraCuuMonAn_Click(object sender, EventArgs e)
        {
            FrmTraCuuMonAn frmtracuumonan = new FrmTraCuuMonAn();
            frmtracuumonan.ShowDialog();
        }

        private void menuitemKhuyenMai_Click(object sender, EventArgs e)
        {
            FrmKhuyenMai frmkhuyenmain = new FrmKhuyenMai();
            frmkhuyenmain.ShowDialog();
            LoadHoaDon(GetMaHD());
        }
        //private void menuitemPhieuDatBan_Click(object sender, EventArgs e)
        //{
        //    FrmPhieuDatBan frmphieudatban = new FrmPhieuDatBan();
        //    frmphieudatban.ShowDialog();
        //}

        private void MenuitemPhieuDatBan_Click_1(object sender, EventArgs e)
        {
            FrmPhieuDatBan frmphieudatban = new FrmPhieuDatBan();
            frmphieudatban.ShowDialog();
            LoadBanKhuVuc();
        }

        private void menuitemTraCuuHoaDon_Click(object sender, EventArgs e)
        {
            FrmTraCuuHoaDon frmtracuuhoadon = new FrmTraCuuHoaDon();
            frmtracuuhoadon.ShowDialog();
        }

        private void menuitemDenBu_Click(object sender, EventArgs e)
        {
            FrmDenBuThietHai frmdenbu = new FrmDenBuThietHai();
            frmdenbu.ShowDialog();
            LoadHoaDon(lbMaHD.Text);
        }

        private void MenuitemChuyenBan_Click(object sender, EventArgs e)
        {
            FrmChuyenBan frmchuyenban = new FrmChuyenBan();
            frmchuyenban.ShowDialog();
            LoadBanKhuVuc();
            LoadHoaDon(lbMaHD.Text);
        }


        private void FrmMain_Load(object sender, EventArgs e)
        {
            LoadBanKhuVuc();
            LoadThucAn();
            LoadHoaDon(GetMaHD());
            
        }

        private void LoadBanKhuVuc()
        {
            try
            {
                flowLayoutPanelBanKhuVuc.Controls.Clear();
                //dtgkhuvuctemp.DataSource = khuvucBL.DSKVBanHang();
                DataTable dtkhuvuctemp = khuvucBL.DSKVBanHang();
                int sokhuvuc = dtkhuvuctemp.Rows.Count;
                int sobantheokhuvuc;
                int sobantheokhuvuc1;

                for (int i = 0; i < sokhuvuc; i++)
                {
                    string tenkhuvuc = dtkhuvuctemp.Rows[i]["TenKV"].ToString();
                    Label lbtenkhuvuc = new Label();
                    lbtenkhuvuc.Text = "----- " + tenkhuvuc + " -----";
                    lbtenkhuvuc.Font = new Font(lbtenkhuvuc.Font, FontStyle.Bold);
                    lbtenkhuvuc.Width = flowLayoutPanelBanKhuVuc.Width - 6;
                    lbtenkhuvuc.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                    flowLayoutPanelBanKhuVuc.Controls.Add(lbtenkhuvuc);

                    string maKV = dtkhuvuctemp.Rows[i]["MaKV"].ToString();
                    DataTable dtbantemp1 = banBL.LayDanhSachBanKhuVuc(maKV);
                    DataTable dtbantemp = banBL.DSBanGopTheoKV(maKV);
                    sobantheokhuvuc1 = dtbantemp1.Rows.Count;
                    sobantheokhuvuc = dtbantemp.Rows.Count;
                    string maban;
                    if (dtbantemp.Rows.Count == dtbantemp1.Rows.Count)
                    {
                        for (int j = 0; j < sobantheokhuvuc1; j++)
                        {
                            newbutton = new Button();
                            newbutton.Text = dtbantemp1.Rows[j]["MaBan"].ToString();
                            newbutton.Height = 50;
                            int trangthaiban = int.Parse(dtbantemp1.Rows[j]["TinhTrang"].ToString());
                            if (trangthaiban == 1)
                                newbutton.BackColor = System.Drawing.Color.White;
                            if (trangthaiban == 2)
                                newbutton.BackColor = System.Drawing.Color.Red;
                            if (trangthaiban == 3)
                                newbutton.BackColor = System.Drawing.Color.Blue;
                            if (trangthaiban == 4)
                            {
                                newbutton.BackColor = System.Drawing.Color.Yellow;
                            }

                            newbutton.MouseDown += new MouseEventHandler(newbutton_MouseDown);
                            flowLayoutPanelBanKhuVuc.Controls.Add(newbutton);

                        }
                    }
                    else
                    {
                        for (int j = 0; j < sobantheokhuvuc; j++)
                        {
                            maban = dtbantemp.Rows[j]["GopBan"].ToString();
                            DataTable checkgopban = banBL.CheckGopBan(maban);
                            DataTable gettinhtrang = banBL.GetTinhTrang(maban);

                            newbutton = new Button();
                            if (checkgopban.Rows.Count == 1)
                                newbutton.Text = maban;
                            else
                            {
                                DataTable getgopban = banBL.GetGopBan(maban);
                                for (int a = 0; a < getgopban.Rows.Count; a++)
                                {
                                    if (a == 0)
                                        newbutton.Text = getgopban.Rows[a]["MaBan"].ToString();
                                    else newbutton.Text += "+" + getgopban.Rows[a]["MaBan"].ToString();
                                }
                            }
                            newbutton.Height = 50;


                            int trangthaiban = int.Parse(gettinhtrang.Rows[0]["TinhTrang"].ToString());
                            if (trangthaiban == 1)
                                newbutton.BackColor = System.Drawing.Color.White;
                            if (trangthaiban == 2)
                                newbutton.BackColor = System.Drawing.Color.Red;
                            if (trangthaiban == 3)
                                newbutton.BackColor = System.Drawing.Color.Blue;
                            if (trangthaiban == 4)
                            {
                                newbutton.BackColor = System.Drawing.Color.Yellow;
                            }

                            newbutton.MouseDown += new MouseEventHandler(newbutton_MouseDown);
                            flowLayoutPanelBanKhuVuc.Controls.Add(newbutton);

                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra:" + ex.ToString());
            }
        }
        
        private void LoadThucAn()
        {
            
            flowLayoutPanelMonAn.Controls.Clear();
            int socot = nhommonBL.GetRow();
            DataTable table = nhommonBL.DSNhomMon();
            string nhommon;
            for (int i = 0; i < table.Rows.Count; i++)
            try
            {
                if (!table.Rows[i]["MaNhom"].ToString().Equals("3"))
                {
                    nhommon = nhommonBL.GetTenNhom(table.Rows[i]["MaNhom"].ToString());
                    Label lbnhommon = new Label();
                    lbnhommon.Text = "----- " + nhommon + " -----";
                    lbnhommon.Font = new Font(lbnhommon.Font, FontStyle.Bold);
                    lbnhommon.TextAlign = ContentAlignment.MiddleCenter;
                    lbnhommon.Width = flowLayoutPanelMonAn.Width - 6;
                    flowLayoutPanelMonAn.Controls.Add(lbnhommon);

                    DataTable table2 = monanBL.DSTenMon(table.Rows[i]["MaNhom"].ToString());
                    int monan = table2.Rows.Count;

                    for (int j = 0; j < monan; j++)
                    {
                        Label lbmonan = new Label();
                        lbmonan.Text = table2.Rows[j]["TenMon"].ToString();
                        lbmonan.Width = 150;
                        lbmonan.Name = "tenmon" + table2.Rows[j]["MaMon"].ToString();
                        TextBox textbox = new TextBox();
                        textbox.Text = "0";
                        textbox.Width = 70;
                        textbox.Name = "mamon" + table2.Rows[j]["MaMon"].ToString();
                        flowLayoutPanelMonAn.Controls.Add(lbmonan);
                        flowLayoutPanelMonAn.Controls.Add(textbox);
                    }
                }
            }
            catch { }
        
        }
        private void LoadHoaDon(string mahd)
        {
            LoadKhuyenMai(lbMaBan.Text);
            lbNgay.Text = String.Format("{0: dd/MM/yyyy}", DateTime.Now);
            //lbKhuyenMai.Text = "0";
            LoadDenBu(mahd);
            lbTongTien.Text = "0";
            lbPhiPhucVu.Text = "4000";
            lbMaHD.Text = mahd;
            float tongtien=0;
            int mamon;

            DataTable dt = cthoadonbl.DSCT_HoaDon(lbMaHD.Text);
            DataTable dt1 = hoadonbl.DSHoaDon(lbMaHD.Text);
            if (lvHoaDon.Items.Count > 0)
                lvHoaDon.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                ListViewItem lvi = new ListViewItem(); 
                mamon  = int.Parse(dt.Rows[i]["MaMon"].ToString());
                DataTable dt2 = monanBL.DSMonTheoMa(mamon.ToString());
                lvi.Text = dt2.Rows[0]["TenMon"].ToString();
                lvi.SubItems.Add(dt.Rows[i]["SoLuong"].ToString());
                lvi.SubItems.Add(dt.Rows[i]["DonGia"].ToString());
                lvi.SubItems.Add(dt.Rows[i]["ThanhTien"].ToString());
                this.lvHoaDon.Items.Add(lvi);
            }
            if (dt1.Rows.Count > 0)
            {
                tongtien = float.Parse(dt1.Rows[0]["TongTien"].ToString());
                lbMaBan.Text = dt1.Rows[0]["MaBan"].ToString();
            }
            lbTongTien.Text = tongtien.ToString();
            if (tongtien > float.Parse(lbPhiPhucVu.Text))
            {
                btThanhToan.Enabled = true;
                btGhiNo.Enabled = true;
            }
            else
            {
                btThanhToan.Enabled = false;
                btGhiNo.Enabled = false;
            }
        }


        private void LoadKhuyenMai(string maban) 
        {
            DataTable dt1 = khuyenmaiBL.TopKhuyenMai();
            DataTable dt2 = banBL.LayDanhSachBanTheoMaBan(maban); 
            DateTime time;
            DateTime ngaybd;
            DateTime ngaykt;
            string makm;
            string makv;
            float khuyenmai;
            if (dt1.Rows.Count > 0)
            {
                time = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yy"));
                ngaybd = Convert.ToDateTime(dt1.Rows[0]["NgayBD"].ToString());
                ngaykt = Convert.ToDateTime(dt1.Rows[0]["NgayKT"].ToString());
                makm = dt1.Rows[0]["MaKM"].ToString();
                if (ngaybd <= time && time <= ngaykt)
                {
                    if (dt2.Rows.Count > 0)
                    {
                        makv = dt2.Rows[0]["MaKV"].ToString();
                        khuyenmai = ctkhuyenmaiBL.TyLeGiam(makv, makm);
                        lbKhuyenMai.Text = (khuyenmai * 100).ToString();
                    }
                }
                else lbKhuyenMai.Text = "0";
            }
        }
        private void LoadDenBu(string mahd)
        {
            DataTable dt = ctdenbubl.DSCT_DenBu(mahd);
            if (dt.Rows.Count > 0)
            {
                lbDenBu.Text = dt.Rows[0]["SoTien"].ToString();
            }
            else lbDenBu.Text = "0";

        }


        private void LoadMon(int maban)
        {

            DataTable dt = banBL.LayDanhSachBanTheoMaBan(maban.ToString());
            lbMaBan.Text = maban.ToString();
            string mamon;
            int soluong;
            string mahd = GetMaHD();
            LoadThucAn();
            LoadHoaDon(mahd);
            if (dt.Rows[0]["TinhTrang"].ToString() == "3")
            {
                btLuu.Enabled = true;
            }
            else if (dt.Rows[0]["TinhTrang"].ToString() == "4")
            {
                btLuu.Enabled = true;

                //Bàn Gọi Món
                try
                {
                    DataTable dt2 = hoadonbl.CheckGoiMon(maban.ToString());
                    mahd = dt2.Rows[0]["MaHD"].ToString();
                    LoadHoaDon(mahd);
                    if (dt2.Rows.Count > 0)
                    {
                        DataTable dt3 = cthoadonbl.DSCT_HoaDon(dt2.Rows[0]["MaHD"].ToString());
                        for (int i = 0; i < dt3.Rows.Count; i++)
                        {
                            mamon = "mamon" + dt3.Rows[i]["MaMon"].ToString();
                            soluong = int.Parse(dt3.Rows[i]["SoLuong"].ToString());
                            flowLayoutPanelMonAn.Controls[mamon].Text = soluong.ToString();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Lỗi Xảy Ra!", "Quản Lý Quán Café", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            else
            {
                btLuu.Enabled = false;
                //LoadHoaDon(mahd);
            }

        }

        private string GetMaHD()
        {
            string mahd;
            string ngay = DateTime.Now.Day.ToString();
            if (int.Parse(ngay) < 10)
                ngay = "0" + ngay;
            string thang = DateTime.Now.Month.ToString();
            if (int.Parse(thang) < 10)
                thang = "0" + thang;

            //string nam = DateTime.Now.Year.ToString();
            //nam = (int.Parse(nam) % 100).ToString();

            string gio = DateTime.Now.Hour.ToString();
            if (int.Parse(gio) < 10)
                gio = "0" + gio;
            string phut = DateTime.Now.Minute.ToString();
            if (int.Parse(phut) < 10)
                phut = "0" + phut;
            //THEM
            string giay = DateTime.Now.Second.ToString();
            if (int.Parse(giay) < 10)
                phut = "0" + giay;
            mahd = ngay + thang + gio + phut + giay;
            return mahd;
        }

        private CT_HoaDonBO LayThongTinCTHD(string mahd, string mamon, int soluong, float gia, float thanhtien)
        {
            CT_HoaDonBO cthoadonBO = new CT_HoaDonBO();
            cthoadonBO.MaHD = mahd;
            cthoadonBO.MaMon = int.Parse(mamon.ToString());
            cthoadonBO.SoLuong = soluong;
            cthoadonBO.DonGia = gia;
            cthoadonBO.ThanhTien = thanhtien;
            return cthoadonBO;
        }
        private string chuyendoi(string dt)
        {
            string date1 = dt.ToString();
            DateTime date = DateTime.ParseExact(date1, new string[] { "MM/dd/yyyy" }, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces);
            string date2 = string.Format("{0: dd/MM/yyyy}", date);
            dt = date2.ToString();
            return dt;
        }

        private HoaDonBO LayThongTinHD(string mahd, int maban, string ngay, float khuyenmai, float denbu, float phiphucvu, float tongtien)
        {
            HoaDonBO hoadonBO = new HoaDonBO();
            hoadonBO.MaHD = mahd;
            hoadonBO.MaBan = maban;
            //hoadonBO.Ngay = Convert.ToDateTime(chuyendoi(ngay));
            hoadonBO.Ngay = DateTime.Now;
            hoadonBO.KhuyenMai = khuyenmai;
            hoadonBO.DenBu = denbu;
            hoadonBO.PhiPhucVu = phiphucvu;
            hoadonBO.TongTien = tongtien;
            return hoadonBO;
        }
        private BanBO LayThongTinBan(int maban, int tinhtrang)
        {
            BanBO banBO = new BanBO();
            banBO.MaBan = maban;
            banBO.TinhTrang = tinhtrang.ToString();

            return banBO;
        }

        void newbutton_MouseDown(object sender, MouseEventArgs e)
        {
            //int maban = int.Parse(sender.ToString().Substring(35));
            string text = sender.ToString().Substring(35);
            int vitri = text.IndexOf("+");
            int maban;
            if (vitri != -1)
            {
                maban = int.Parse(text.Substring(0, vitri));
            }
            else maban = int.Parse(text.ToString());
            if (e.Clicks == 1)
            { 
                LoadMon(maban);
            }
            if (e.Clicks == 2)
            {
                DataTable dtbantheomabantemp = banBL.LayDanhSachBanTheoMaBan(maban.ToString());

                int tinhtrangdau = int.Parse(dtbantheomabantemp.Rows[0]["TinhTrang"].ToString());
                string tentinhtrangcu = "";
                string tentinhtrangmoi = "";
                if (tinhtrangdau == 1)
                {
                    tentinhtrangcu = "Trống";
                    tentinhtrangmoi = "Có Khách (Chưa GM)";
                }
                if (tinhtrangdau == 2)
                {
                    tentinhtrangcu = "Đã Đặt";
                    tentinhtrangmoi = "Có Khách (Chưa GM)";
                }
                if (tinhtrangdau == 3)
                {
                    tentinhtrangcu = "Có Khách (Chưa GM)";
                    tentinhtrangmoi = "Trống";
                }
                if (tinhtrangdau != 4)
                {
                    if (MessageBox.Show("Bàn [" + maban + "] Đang Ở Tình Trạng [" + tentinhtrangcu + "]. Bạn Có Chắc Chắn Chuyển Qua Trạng Thái [" + tentinhtrangmoi + "] ?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (tinhtrangdau == 1 || tinhtrangdau == 2)
                        {
                            if (banBL.CapNhatTinhTrangBan(maban, 3))
                            {
                                LoadBanKhuVuc();
                                btLuu.Enabled = true;
                            }
                        }
                        if (tinhtrangdau == 3)
                        {
                            if (banBL.CapNhatTinhTrangBan(maban, 1))
                            {
                                LoadBanKhuVuc();
                                btLuu.Enabled = false;
                            }
                        }
                    }
                }
            }
        }                


        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            FrmMain_Load(sender, e);
        }


       
        private void btLuu_Click(object sender, EventArgs e)
        {
            DataTable table = monanBL.DSMonAn();
            string maban = lbMaBan.Text;
            DataTable dt = banBL.LayDanhSachBanTheoMaBan(maban.ToString());
            string tenmon;
            float gia;
            int soluong;
            float thanhtien;
            float tongtien=float.Parse(lbPhiPhucVu.Text);
            int tinhtrang = 0;
            float tiendu = 0;
            HoaDonBO hoadonBO;
            string mon;
            string mamon;
            int sosanhmamon=0;
            if (dt.Rows[0]["TinhTrang"].ToString() == "3")
            {
                hoadonBO = LayThongTinHD(lbMaHD.Text, int.Parse(lbMaBan.Text), lbNgay.Text, float.Parse(lbKhuyenMai.Text), float.Parse(lbDenBu.Text), float.Parse(lbPhiPhucVu.Text), tongtien);
                if (hoadonbl.ThemHDPhu(hoadonBO))
                { 
                }
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    mon = table.Rows[i]["MaMon"].ToString();
                    mamon = "mamon" + mon;
                    if (flowLayoutPanelMonAn.Controls[mamon] != null)
                    { 
                    if (flowLayoutPanelMonAn.Controls[mamon].Text != "0")
                    {
                        sosanhmamon++;
                        //Lấy Thông Tin Món
                        DataTable table2 = monanBL.DSMonTheoMa(mon);
                        tenmon = table2.Rows[0]["TenMon"].ToString();
                        gia = float.Parse(table2.Rows[0]["GiaThuong"].ToString());
                        soluong = int.Parse(flowLayoutPanelMonAn.Controls[mamon].Text);
                        thanhtien = soluong * gia;
                        CT_HoaDonBO cthoadonBO = LayThongTinCTHD(lbMaHD.Text, mon, soluong, gia, thanhtien);
                        
                        //Thêm CT_Hoadon
                        if (cthoadonbl.ThemCTHD(cthoadonBO))
                        {
                        }
                    }
                    }
                }

                if (sosanhmamon == 0)
                {
                    hoadonbl.XoaHD(lbMaHD.Text);
                    MessageBox.Show("Bạn Chưa Chọn Món Nào!", "Quản Lý Quán Café", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    DataTable dt1 = cthoadonbl.DSCT_HoaDon(lbMaHD.Text);
                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        tongtien += float.Parse(dt1.Rows[j]["ThanhTien"].ToString());

                    }
                    tongtien = tongtien - tongtien * float.Parse(lbKhuyenMai.Text) / 100 + float.Parse(lbDenBu.Text);
                    tiendu = tongtien % 100;
                    tongtien -= tiendu;
                    hoadonBO = LayThongTinHD(lbMaHD.Text, int.Parse(lbMaBan.Text), lbNgay.Text, float.Parse(lbKhuyenMai.Text), float.Parse(lbDenBu.Text), float.Parse(lbPhiPhucVu.Text), tongtien);
                    //Thêm HoaDon
                    if (hoadonbl.SuaHD(hoadonBO))
                    {

                        MessageBox.Show("Thêm Món Thành Công!", "Quản Lý Quán Café", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    //Cập nhật lại trạng thái bàn
                    tinhtrang = 4;
                    BanBO banbo = LayThongTinBan(int.Parse(lbMaBan.Text), tinhtrang);
                    banBL.SuaTinhTrang(banbo);

                    LoadBanKhuVuc();
                }
            }
            else if (dt.Rows[0]["TinhTrang"].ToString() == "4")
            {

                //Kiểm tra giảm món về 0
                DataTable dt2 = cthoadonbl.DSCT_HoaDon(lbMaHD.Text);
                
                    //Sửa món
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        mon = table.Rows[i]["MaMon"].ToString();
                        mamon = "mamon" + mon;
                        if (flowLayoutPanelMonAn.Controls[mamon] != null)
                        {
                            if (flowLayoutPanelMonAn.Controls[mamon].Text != "0")
                            {
                                sosanhmamon++;
                                //Lấy Thông Tin Món
                                DataTable table2 = monanBL.DSMonTheoMa(mon);
                                tenmon = table2.Rows[0]["TenMon"].ToString();
                                gia = float.Parse(table2.Rows[0]["GiaThuong"].ToString());
                                soluong = int.Parse(flowLayoutPanelMonAn.Controls[mamon].Text);
                                thanhtien = soluong * gia;
                                CT_HoaDonBO cthoadonBO = LayThongTinCTHD(lbMaHD.Text, mon, soluong, gia, thanhtien);

                                //Thêm CT_Hoadon
                                if (!cthoadonbl.CheckCTMonAn(lbMaHD.Text, mon))
                                {
                                    if (cthoadonbl.ThemCTHD(cthoadonBO))
                                    {

                                    }
                                }
                                //Sửa CT_HoaDon
                                else
                                {
                                    if (cthoadonbl.SuaCTMonHD(cthoadonBO))
                                    {

                                    }
                                }

                            }
                        }
                    }
                    if (sosanhmamon == 0)
                    {
                        MessageBox.Show("Không Thể Bỏ Hết Món Được!", "Quản Lý Quán Café", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadMon(int.Parse(lbMaBan.Text));
                    }
                    else
                    {
                        for (int a = 0; a < dt2.Rows.Count; a++)
                        {
                            mon = dt2.Rows[a]["MaMon"].ToString();
                            mamon = "mamon" + mon;
                            if (flowLayoutPanelMonAn.Controls[mamon] != null)
                            {
                                if (flowLayoutPanelMonAn.Controls[mamon].Text == "0")
                                {
                                    cthoadonbl.XoaCTHoaDon(lbMaHD.Text, mon);
                                }
                            }
                        }
                        //Lay lại ds hóa đơn
                        DataTable dt1 = cthoadonbl.DSCT_HoaDon(lbMaHD.Text);
                        for (int j = 0; j < dt1.Rows.Count; j++)
                        {
                            tongtien += float.Parse(dt1.Rows[j]["ThanhTien"].ToString());
                        }
                        tongtien = tongtien - tongtien * float.Parse(lbKhuyenMai.Text) / 100 + float.Parse(lbDenBu.Text);
                        tiendu = tongtien % 100;
                        tongtien -= tiendu;
                        hoadonBO = LayThongTinHD(lbMaHD.Text, int.Parse(lbMaBan.Text), lbNgay.Text, float.Parse(lbKhuyenMai.Text), float.Parse(lbDenBu.Text), float.Parse(lbPhiPhucVu.Text), tongtien);
                        //Sửa HoaDon
                        if (hoadonbl.SuaHD(hoadonBO))
                        {

                            MessageBox.Show("Sửa Món Thành Công!", "Quản Lý Quán Café", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    
            }
            lbTongTien.Text = tongtien.ToString();
            LoadHoaDon(lbMaHD.Text);
        }

     

        private void btHuy_Click(object sender, EventArgs e)
        {
            LoadMon(int.Parse(lbMaBan.Text));
        }

        private void btThanhToan_Click(object sender, EventArgs e)
        {
            int tinhtrang;
            string maHD = lbMaHD.Text;
            DialogResult result = MessageBox.Show("Bạn Có Chắc Muốn Thanh Toán Bàn ["+lbMaBan.Text+"] Không?", "Quản Lý Quán Café", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                tinhtrang = 1;
                //hoadonbl.ThanhToanHD(tinhtrang.ToString(),maHD);
                hoadonbl.ThanhToanHDPhu(tinhtrang.ToString(), lbDenBu.Text, maHD);
                tinhtrang = 3;
                BanBO banbo = LayThongTinBan(int.Parse(lbMaBan.Text), tinhtrang);
                banBL.SuaTinhTrang(banbo);
                FrmMain_Load(sender, e);
                LoadHoaDon(GetMaHD());

                DialogResult _result = MessageBox.Show("Thanh Toán Thành Công! Bạn Có Muốn In Hóa Đơn Không?", "Quản Lý Quán Café", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (_result == DialogResult.Yes)
                {
                    InHoaDon inhoadon = new InHoaDon();
                    ParameterValues a = new ParameterValues();
                    ParameterDiscreteValue b = new ParameterDiscreteValue();

                    DataTable datatable = cthoadonbl.LayDanhSachTraCuuChiTietHoaDon(maHD);
                    DataTable _datatable = hoadonbl.DSHoaDon(maHD);
                    

                    inhoadon.SetDataSource(datatable);

                    b.Value = "";
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["KhachHang"].ApplyCurrentValues(a);
                    
                    b.Value = _datatable.Rows[0]["MaHD"].ToString();
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["MaHD"].ApplyCurrentValues(a);

                    b.Value = _datatable.Rows[0]["MaBan"].ToString();
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["MaBan"].ApplyCurrentValues(a);

                    b.Value = _datatable.Rows[0]["Ngay"].ToString();
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["Ngay"].ApplyCurrentValues(a);

                    b.Value = _datatable.Rows[0]["KhuyenMai"].ToString() + " %";
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["KhuyenMai"].ApplyCurrentValues(a);

                    b.Value = _datatable.Rows[0]["DenBu"].ToString() + " Đồng";
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["DenBu"].ApplyCurrentValues(a);

                    b.Value = _datatable.Rows[0]["PhiPhucVu"].ToString() + " Đồng";
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["PhiPhucVu"].ApplyCurrentValues(a);

                    b.Value = _datatable.Rows[0]["TongTien"].ToString() + " Đồng";
                    a.Add(b);
                    inhoadon.DataDefinition.ParameterFields["TongTien"].ApplyCurrentValues(a);

                    FrmInHoaDon frminhoadon = new FrmInHoaDon(inhoadon);
                    frminhoadon.ShowDialog();
                    frminhoadon.Dispose();


                }
            }
        }


        
        private void btGhiNo_Click(object sender, EventArgs e)
        {   
            FrmGhiNo ghino = new FrmGhiNo();
            ghino.GetNoMoi = lbTongTien.Text;
            ghino.GetMaBan = lbMaBan.Text;
            ghino.GetMaHD = lbMaHD.Text;
            ghino.ShowDialog();
            FrmMain_Load(sender, e);
        }

        private void menuitemTraCuuDoanhThuThang_Click(object sender, EventArgs e)
        {
            FrmTraCuuDoanhThuThang frmtracuudoanhthuthang = new FrmTraCuuDoanhThuThang();
            frmtracuudoanhthuthang.ShowDialog();
        }

        private void btnTimBanTren_Click(object sender, EventArgs e)
        {
            try
            {
                flowLayoutPanelBanKhuVuc.Controls.Clear();
                
                DataTable datatemp = banBL.LayDanhSachTimKiemBanTheoMaBan(txtMaBan.Text);               

                for (int i = 0; i < 1; i++)
                {
                    string tenkhuvuc = datatemp.Rows[i]["TenKV"].ToString();
                    Label lbtenkhuvuc = new Label();
                    lbtenkhuvuc.Text = "----- " + tenkhuvuc + " -----";
                    lbtenkhuvuc.Font = new Font(lbtenkhuvuc.Font, FontStyle.Bold);
                    lbtenkhuvuc.Width = flowLayoutPanelBanKhuVuc.Width - 6;
                    lbtenkhuvuc.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                    flowLayoutPanelBanKhuVuc.Controls.Add(lbtenkhuvuc);                    
                    
                    for (int j = 0; j < 1; j++)
                    {
                        newbutton = new Button();
                        newbutton.Text = datatemp.Rows[j]["MaBan"].ToString();
                        newbutton.Height = 50;
                        int trangthaiban = int.Parse(datatemp.Rows[j]["TinhTrang"].ToString());
                        if (trangthaiban == 1)
                            newbutton.BackColor = System.Drawing.Color.White;
                        if (trangthaiban == 2)
                            newbutton.BackColor = System.Drawing.Color.Red;
                        if (trangthaiban == 3)
                            newbutton.BackColor = System.Drawing.Color.Blue;
                        if (trangthaiban == 4)
                        {
                            newbutton.BackColor = System.Drawing.Color.Yellow;
                        }

                        newbutton.MouseDown += new MouseEventHandler(newbutton_MouseDown);
                        flowLayoutPanelBanKhuVuc.Controls.Add(newbutton);

                    }
                }

            }
            catch { }
        }

        private void txtMaBan_TextChanged(object sender, EventArgs e)
        {
            if (txtMaBan.Text.Equals(""))
                LoadBanKhuVuc();
        }

        private void txtTenMon_TextChanged(object sender, EventArgs e)
        {
            if (txtTenMon.Text.Equals(""))
                LoadThucAn();
        }

        private void btnTimMon_Click(object sender, EventArgs e)
        {
            flowLayoutPanelMonAn.Controls.Clear();
            int socot = nhommonBL.GetRow();
            DataTable datatable = monanBL.LayKetQuaDanhSachTraCuuMonAnTheoTen(txtTenMon.Text);
            string nhommon;
            for (int i = 0; i < datatable.Rows.Count; i++)
                try
                {
                    Label lbnhommon = new Label();
                    nhommon = datatable.Rows[i]["TenNhom"].ToString();
                    lbnhommon.Text = "----- " + nhommon + " -----";
                    lbnhommon.Font = new Font(lbnhommon.Font, FontStyle.Bold);
                    lbnhommon.TextAlign = ContentAlignment.MiddleCenter;
                    lbnhommon.Width = flowLayoutPanelMonAn.Width - 6;
                    flowLayoutPanelMonAn.Controls.Add(lbnhommon);

                    int monan = datatable.Rows.Count;

                    for (int j = 0; j < monan; j++)
                    {
                        Label lbmonan = new Label();
                        lbmonan.Text = datatable.Rows[j]["TenMon"].ToString();
                        lbmonan.Width = 150;
                        lbmonan.Name = "tenmon" + datatable.Rows[j]["MaMon"].ToString();
                        TextBox textbox = new TextBox();
                        textbox.Text = "0";
                        textbox.Width = 70;
                        textbox.Name = "mamon" + datatable.Rows[j]["MaMon"].ToString();
                        flowLayoutPanelMonAn.Controls.Add(lbmonan);
                        flowLayoutPanelMonAn.Controls.Add(textbox);
                    }
                }
                catch { }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void menuitemThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Chắc Chắn Muốn Thoát Khỏi Chương Trình?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Dispose();
                dangnhap.Show();
                dangnhap.XoaThongTinDangNhap();
            }
        }

        private void MenuitemGopTach_Click(object sender, EventArgs e)
        {
            FrmGopBan frmgopban = new FrmGopBan();
            frmgopban.ShowDialog();
            LoadBanKhuVuc();
        }
    }
}
